package ui;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.wb.swt.SWTResourceManager;

import each_class.Album;
import each_class.SongClass;
import ui.global.Current;
import ui.global.Operation;
import ui.global.UIColor;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;


//纯UI类
public class MusicViewItem extends Composite implements MouseTrackListener, MouseListener{

	private SongClass music;
	private SwitchButton favoriteButton;
	private final Font font = new Font(getDisplay(), "Microsoft YaHei UI", 11, SWT.NORMAL);
	private Label time;
	private Label album;
	private Label singer;
	private Label musicName;
	
	public MusicViewItem(Composite parent, int style) {
		super(parent, style);
		setSize(1210, 32);
		setLayout(new FormLayout());
		favoriteButton = new SwitchButton(this, SWT.NONE) {
			@Override
			public void mouseUp(MouseEvent e) {
				super.mouseUp(e);
				if (state) {
					ui.global.Operation.favorite(music);
				}else {
					ui.global.Operation.unFavorite(music);
				}
				MusicViewItem.this.layout();
			}
		};
		FormData fd_favoriteButton = new FormData();
		fd_favoriteButton.height = 16;
		fd_favoriteButton.width = 16;
		fd_favoriteButton.top = new FormAttachment(0, 5);
		fd_favoriteButton.left = new FormAttachment(0, 29);
		fd_favoriteButton.bottom = new FormAttachment(100, -11);
		favoriteButton.setLayoutData(fd_favoriteButton);
		
		musicName = new Label(this, SWT.NONE);
		FormData fd_musicName = new FormData();
		fd_musicName.top = new FormAttachment(0, 2);
		fd_musicName.left = new FormAttachment(0, 50);
		musicName.setLayoutData(fd_musicName);
		musicName.setFont(font);
		
		singer = new Label(this, SWT.NONE);
		FormData fd_singer = new FormData();
		fd_singer.top = new FormAttachment(0, 2);
		fd_singer.left = new FormAttachment(0, 401);
		singer.setLayoutData(fd_singer);
		singer.setFont(font);
		
		album = new Label(this, SWT.NONE);
		FormData fd_album = new FormData();
		fd_album.top = new FormAttachment(0, 2);
		fd_album.left = new FormAttachment(0, 750);
		album.setLayoutData(fd_album);
		album.setFont(font);
		
		time = new Label(this, SWT.NONE);
		FormData fd_time = new FormData();
		fd_time.top = new FormAttachment(0, 2);
		fd_time.left = new FormAttachment(0, 1054);
		time.setLayoutData(fd_time);
		time.setFont(font);
		
		
		ScrolledView scrolledView  = ((ScrolledView)getParent());
		addMouseWheelListener(scrolledView);
		musicName.addMouseWheelListener(scrolledView);
		singer.addMouseWheelListener(scrolledView);
		album.addMouseWheelListener(scrolledView);
		time.addMouseWheelListener(scrolledView);
	}
	
	public void initail(SongClass music) {
		this.music = music;
		favoriteButton.Initial(
		SWTResourceManager.getImage(MusicViewItem.class, "/image/favorite_actived.png"),
		SWTResourceManager.getImage(MusicViewItem.class, "/image/favorite_actived.png"),
		SWTResourceManager.getImage(MusicViewItem.class, "/image/favorite_deactive.png"),
		SWTResourceManager.getImage(MusicViewItem.class, "/image/favorite_active.png"),
		music.isFavorite());
		if(music.getSong_name() != null)
		musicName.setText(music.getSong_name());
		if(music.getSingerString() != null)
		singer.setText(music.getSingerString());
		if(music.getAlbum_name() != null)
		album.setText(music.getAlbum_name());
		if(music.getDuration() != null)
		time.setText(music.getDuration());
		
		
		MouseTrackAdapter mouseTrackAdapter = new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				((Label)e.widget).setForeground(ui.global.UIColor.GREEN);
			}
			@Override
			public void mouseExit(MouseEvent e) {
				((Label)e.widget).setForeground(ui.global.UIColor.DARK);
			}
		};
		
		singer.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				String search = ((Label)e.widget).getText();
				ui.global.Operation.search(search);
			}
		});
		singer.addMouseTrackListener(mouseTrackAdapter);
		
		album.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				new Thread() {
					@Override
					public void run() {
						Album album = Operation.getAlbum(music);
						getDisplay().asyncExec(new Runnable() {
							
							@Override
							public void run() {
								Current.getMusicListUI().active(album);
							}
						});
						
					}
				}.start();
			}
		});
		album.addMouseTrackListener(mouseTrackAdapter);
		addMouseListener(this);
		addMouseTrackListener(this);
		
	}

	@Override
	public void mouseEnter(MouseEvent e) {
		setBackground(UIColor.GRAY);
	}

	@Override
	public void mouseExit(MouseEvent e) {
		setBackground(UIColor.PUREWHITE);
	}

	@Override
	public void mouseHover(MouseEvent e) {}

	@Override
	public void mouseDoubleClick(MouseEvent e) {
		System.out.println(getBounds());
		ui.global.Operation.add_PlayMusic(music);
	}

	@Override
	public void mouseDown(MouseEvent e) {}

	@Override
	public void mouseUp(MouseEvent e) {
		
	}
	
	@Override
	public void setBackground(Color color) {
		super.setBackground(color);
		musicName.setBackground(color);
		singer.setBackground(color);
		album.setBackground(color);
		time.setBackground(color);
	}


}
